package com.greedy.tank;

import java.awt.*;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-07-04
 * Description:
 */
public class Tank {

    /**
     * 坐标
     */
    private int x, y;

    /**
     * 随便定义一个坦克方向
     */
    private Dir dir = Dir.DOWN;

    /**
     * 坦克的速度
     */
    private static final int SPEED = 5;


    /**
     * 坦克初始化的时候不动
     */
    private boolean moving = false;

    /**
     * 坦克窗口对象的引用
     */
    private TankFrame tf = null;


    /**
     * getter
     *
     * @return x
     */
    public Dir getDir() {
        return dir;
    }

    /**
     * setter
     *
     * @param dir 方向
     */
    public void setDir(Dir dir) {
        this.dir = dir;
    }

    /**
     * getter
     * @return x
     */
    public boolean isMoving() {
        return moving;
    }

    /**
     * setter
     * @param moving 是否移动
     */
    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    /**
     * 坦克的构造方法
     *
     * @param x   坐标x
     * @param y   坐标y
     * @param dir 方向
     */
    public Tank(int x, int y, Dir dir, TankFrame tf) {
        super();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
    }

    /**
     * getter
     *
     * @param g 画笔
     */
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.YELLOW);
        g.fillRect(x, y, 50, 50);
        g.setColor(c);
        this.move();
    }

    /**
     * 坦克移动
     */
    private void move() {
        if (!moving) {
            return;
        }
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }
    }

    /**
     * 开火
     */
    public void fire() {
        tf.bullet = new Bullet(this.x, this.y, this.dir);
    }
}
